import 'dart:typed_data';
import 'package:bluetooth_protocols_plugin/src/tools/extension_enum.dart';

import '../../../base/realtime/realtime_info_base.dart';
import '../../../tools/utils.dart';
import '../../proto/ble.pb.dart';

// MCU档位状态
enum McuGearStatus {
  MCU_gear_N(0), //空挡
  MCU_gear_F_lowspeed(1), //前进低速档
  MCU_gear_F_midspeed(2), //前进中速档
  MCU_gear_F_highspeed(3), //前进高速档
  MCU_gear_R(4), //倒档
  MCU_gear_P(5), //P档
  MCU_gear_hold(6), //边撑档
  MCU_gear_cuirse(7), //巡航档
  MCU_gear_F_race(8); //前进竞赛档

  final int rawValue;
  const McuGearStatus(this.rawValue);
  // 通过 rawValue 获取枚举类型
  static McuGearStatus? getTypeByNum(int rawValue) => McuGearStatus.values
      .firstWhereOrNull((activity) => activity.rawValue == rawValue);
}

// Huari实时信息
class HuariRealtimeInfo extends RealtimeInfoBase {
  int speed = 0; // 时速表
  int odometer = 0; // 总里程表
  int mcuTemperature = 0; // 电机控制器温度
  int motorTemperature = 0; // 电机温度
  int motorTorque = 0; // 电机输出扭矩
  int motorPower = 0; // 电机输出功率
  int bmsSoc = 0; // 电池电量
  int bmsSoh = 0; // 电池健康度
  int bmsTemperature = 0; // 电池温度
  double currentMeter = 0.0; // 当前里程表
  int currentMode = 0; // 当前骑行模式
  bool bmsErrorShortCircuit = false; // BMS电池短路故障
  bool bmsErrorGroupOverVoltage = false; // 电池组过压保护
  bool bmsErrorGroupLowVoltage = false; // 电池组低压保护
  bool bmsErrorMosOverTemp = false; // MOS管过温
  bool bmsErrorDischargeOverTemp = false; // 放电高温保护
  bool bmsErrorDischargeOverCurrent = false; // 放电过流保护
  bool bmsErrorDischargeMosDefect = false; // 放电MOS管损坏
  bool mcuPhaseCurrentOver = false; // 相电流过流故障
  bool mcuBusbarVoltHigh = false; // 直流母线过压故障
  bool mcuBusbarVoltLow = false; // 直流母线欠压故障
  bool mcuLogicVoltLow = false; // MCU低压电源欠压故障
  bool mcuLogicVoltHigh = false; // MCU低压电源过压故障
  bool lockStatus = false; // 锁车状态
  McuGearStatus mcuGearStatus = McuGearStatus.MCU_gear_N; // MCU档位状态
  int rollingCnt = 0; // 滚动计数

  // 从字节数据解析
  HuariRealtimeInfo.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
    if (bytes.length < minLength) {
      throw ArgumentError(
          '字节数组长度不足，无法解析 HuariRealtimeInfo, bytes.length: ${bytes.length}, minLength: $minLength');
    }
    DisplayContent displayContent = DisplayContent.fromBuffer(bytes);
    speed = displayContent.speed;
    odometer = displayContent.odometer * 2; // 里程表值乘以2，系数为2
    mcuTemperature =
        displayContent.mcuTemperature - 40; // 温度值偏移，实际温度 = 读取值 - 40
    motorTemperature = displayContent.motorTemperature - 40;
    motorTorque = displayContent.motorTorque > 6500
        ? 0
        : (displayContent.motorTorque * 0.1)
            .toInt(); // 扭矩值大于6500时，视为无效值，设置为0，系数为0.1
    motorPower = displayContent.motorPower;
    bmsSoc = displayContent.bmsSoc;
    bmsSoh = displayContent.bmsSoh;
    bmsTemperature = displayContent.bmsTemperature - 40;
    currentMeter = displayContent.cuttentMeter * 0.1; // 里程表值乘以0.1，系数为0.1
    currentMode = displayContent.cuttentMode;
    bmsErrorShortCircuit = displayContent.bMSErrorShortCircuit;
    bmsErrorGroupOverVoltage = displayContent.bMSErrorGroupOverVoltage;
    bmsErrorGroupLowVoltage = displayContent.bMSErrorGroupLowVoltage;
    bmsErrorMosOverTemp = displayContent.bMSErrorMOSOverTemp;
    bmsErrorDischargeOverTemp = displayContent.bMSErrorDisChargeOverTemp;
    bmsErrorDischargeOverCurrent = displayContent.bMSErrorDisChargeOverCurrent;
    bmsErrorDischargeMosDefect = displayContent.bMSErrorDischargeMOSDefect;
    mcuPhaseCurrentOver = displayContent.mCUPhasMCUCurrentOver;
    mcuBusbarVoltHigh = displayContent.mCUBusbarVoltHigh;
    mcuBusbarVoltLow = displayContent.mCUBusbarVoltLow;
    mcuLogicVoltLow = displayContent.mCULogicVoltlow;
    mcuLogicVoltHigh = displayContent.mCULogicVoltHigh;
    // 取反的原因是，车辆锁车状态与实际状态相反，0x01表示解锁，0x00表示上锁
    lockStatus = !displayContent.vehicleLockstate;
    mcuGearStatus =
        McuGearStatus.getTypeByNum(displayContent.mCUGearStatus.value) ??
            McuGearStatus.MCU_gear_N;
    rollingCnt = displayContent.rollingCnt;
  }

  // 将对象转换为字节数据
  @override
  Uint8List toBytes() {
    return DisplayContent(
      speed: speed,
      odometer: odometer,
      mcuTemperature: mcuTemperature,
      motorTemperature: motorTemperature,
      motorTorque: motorTorque,
      motorPower: motorPower,
      bmsSoc: bmsSoc,
      bmsSoh: bmsSoh,
      bmsTemperature: bmsTemperature,
      cuttentMeter: (currentMeter * 10).toInt(), // 里程表值乘以10，系数为0.1
      cuttentMode: currentMode,
      bMSErrorShortCircuit: bmsErrorShortCircuit,
      bMSErrorGroupOverVoltage: bmsErrorGroupOverVoltage,
      bMSErrorGroupLowVoltage: bmsErrorGroupLowVoltage,
      bMSErrorMOSOverTemp: bmsErrorMosOverTemp,
      bMSErrorDisChargeOverTemp: bmsErrorDischargeOverTemp,
      bMSErrorDisChargeOverCurrent: bmsErrorDischargeOverCurrent,
      bMSErrorDischargeMOSDefect: bmsErrorDischargeMosDefect,
      mCUPhasMCUCurrentOver: mcuPhaseCurrentOver,
      mCUBusbarVoltHigh: mcuBusbarVoltHigh,
      mCUBusbarVoltLow: mcuBusbarVoltLow,
      mCULogicVoltlow: mcuLogicVoltLow,
      mCULogicVoltHigh: mcuLogicVoltHigh,
      vehicleLockstate: lockStatus,
      mCUGearStatus: MCU_gear_status_TYPE.valueOf(mcuGearStatus.rawValue),
    ).writeToBuffer();
  }

  @override
  int dataIntValue() {
    return Utils.computeDataIntVal(toBytes());
  }

  @override
  int dataLength() {
    return toBytes().length;
  }

  @override
  int get minLength => 0;

  @override
  Map<String, dynamic> toMap() {
    return {
      'speed': speed,
      'odometer': odometer,
      'mcuTemperature': mcuTemperature,
      'motorTemperature': motorTemperature,
      'motorTorque': motorTorque,
      'motorPower': motorPower,
      'bmsSoc': bmsSoc,
      'bmsSoh': bmsSoh,
      'bmsTemperature': bmsTemperature,
      'currentMeter': currentMeter,
      'currentMode': currentMode,
      'bmsErrorShortCircuit': bmsErrorShortCircuit,
      'bmsErrorGroupOverVoltage': bmsErrorGroupOverVoltage,
      'bmsErrorGroupLowVoltage': bmsErrorGroupLowVoltage,
      'bmsErrorMosOverTemp': bmsErrorMosOverTemp,
      'bmsErrorDischargeOverTemp': bmsErrorDischargeOverTemp,
      'bmsErrorDischargeOverCurrent': bmsErrorDischargeOverCurrent,
      'bmsErrorDischargeMosDefect': bmsErrorDischargeMosDefect,
      'mcuPhaseCurrentOver': mcuPhaseCurrentOver,
      'mcuBusbarVoltHigh': mcuBusbarVoltHigh,
      'mcuBusbarVoltLow': mcuBusbarVoltLow,
      'mcuLogicVoltLow': mcuLogicVoltLow,
      'mcuLogicVoltHigh': mcuLogicVoltHigh,
      'lockStatus': lockStatus,
      'mcuGearStatus': mcuGearStatus.rawValue,
      'rollingCnt': rollingCnt,
    };
  }
}
